package com.personal.cuncunxing.module.home.live;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.personal.cuncunxing.R;
import com.personal.cuncunxing.common.OkHttpUtils;
import com.personal.cuncunxing.common.ResultCallback;
import com.personal.cuncunxing.module.home.BaseFragment;
import com.personal.cuncunxing.module.home.pojo.ContactBeanConvert;
import com.personal.cuncunxing.module.home.pojo.Info;
import com.personal.cuncunxing.module.home.pojo.Info2tv;

import java.io.IOException;

import okhttp3.Request;

/**
 * Description:
 * Created by kode on 2020/5/26.
 */
public class LiveFragment extends BaseFragment {
    private static final String TAG = "LiveFragment";
    private RecyclerView recycler_view;
    private LiveRVAdapter liveRVAdapter;
    private JSONObject jsonObject = null;
    private VideoView liveVideo;

    public VideoView getLiveVideo() {
        return liveVideo;
    }

    @Override
    protected int setLayout() {
        return R.layout.home_live_fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated: ");
        isViewCreated = true;
        lazyLoad();
        recycler_view = view.findViewById(R.id.home_live_rv);
        liveVideo = view.findViewById(R.id.live_video_view);
        //设置线性布局
        recycler_view.setLayoutManager(new LinearLayoutManager(getContext()));
        //设置适配器
        liveRVAdapter = new LiveRVAdapter();
        recycler_view.setAdapter(liveRVAdapter);
        onPlay();
    }

    /**
     * 播放电视
     */
    private void onPlay() {
        liveRVAdapter.setOnTvPlay(new LiveSubRVAdapter.OnTvPlay() {
            @Override
            public void OnPlay(String url) {
                Log.d(TAG, "OnPlay: " + url);
                liveVideo.setVideoPath(url);
                liveVideo.start();
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG, "setUserVisibleHint: ->" + isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    //懒加载
    @Override
    protected void lazyLoad() {
        // //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,
        // 必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
            Log.d(TAG, "lazyLoad:->加载数据");
        }
    }

    /**
     * 加载电视直播数据
     */
    private void loadData() {
        String url = "http://175.24.55.218/api/tvLive.json";
        OkHttpUtils.getInstance(getContext()).getAsyncHttp(url, new ResultCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String str) throws IOException {
                //Log.d(TAG, "onResponse: " + str);
                jsonObject = JSON.parseObject(str);
                //Log.d(TAG, "onResponse: " + jsonObject.getString("info"));
//              //问题一：Json解析难点
                Info.getInstance().setInfo2tvList(ContactBeanConvert.getInstance().convertToEntityProperty(jsonObject.getString("info"), Info2tv.class));
                Log.d(TAG, "onResponse: " + Info.getInstance().getInfo2tvList().get(0).getT_type());
                Log.d(TAG, "onResponse: " + Info.getInstance().getInfo2tvList().get(0).getTv().size());
                liveRVAdapter.updateUI();
            }
        });
    }
}
